<?php 
class UserAction extends ManagerAction{
	function login(){
		if($_POST){
			$login_info = Think::instance('UserCheck')->login();
			if($login_info['flag']){
				$this->redirect('Index/index');
			}else{
				$this->assign('jumpUrl',U('User/login'));
				$this->error($login_info['msg']);
			}	
		}else{
			$this->display();
		}
	}
	function logout(){
		Think::instance('UserCheck')->logout();
		$this->redirect('Manager-User/login');
	}
	/**
	 * 查看用户列表带过滤器
	 */
	function ulist(){
		$where = "state>=0";
		if($_POST){//如果有post则是过滤器分页的时候存session
			$name = trim($_POST['name']);
			$auth = intval($_POST['auth']);
			Session::set('ul_name',$name);
			Session::set('ul_auth',$auth);
			if($name){
				$where .= " AND (email LIKE '%$name%' OR name LIKE '%$name%')";
			}
			if(Session::get('auth') == 1){
				if($auth){	$where .= " AND `auth` = '$auth'";}
			}
		}else{
			if(Session::get('ul_name')){
				$where .= " AND (email LIKE '%".Session::get('ul_name')."%' OR name LIKE '%".Session::get('ul_name')."%')";
			}
		}
		$User = M('User');
		$count = $User->where($where)->count();
		$Page = new Page($count,C('PAGE_COUNT'));
		$limit = "{$Page->firstRow},{$Page->listRows}";
		$list = $User->where($where)->limit($limit)->select();
		$this->assign('page',$Page->show());
		$this->assign('list',$list);
		$this->display();
	}
	function add(){
		if($_POST){
			$User = D('User');
			if($User->create()){
				$User->add();
				$this->success('添加成功 ');
			}else{
				$this->assign('jumpUrl',U('user/add'));
				$this->error($User->geterror());
			}
		}else{
			$this->display();
		}
	}
	function update(){
		if($_POST){
			if(empty($_POST['password'])){
				unset($_POST['password'],$_POST['password_repeat']);
			}
			$uid = intval(trim($_POST['uid']));
			$User = D('user');
			$where = "uid={$uid}";
			if($User->create()){
				$User->where($where)->save();
				$this->success('修改成功');
			}else{
				$this->error($User->geterror());
			}
		}else{
			$uid = intval(trim($_GET['uid']));
			$user_info = M('user')->where("uid = $uid")->find();
			$this->assign('user_info',$user_info);
			$this->display();
		}
	}
	function getDepartmentList(){
		$pid = trim($_GET['pid']);
		if(!is_numeric($pid))exit;
		$where = "pid = $pid AND type=5";
		$dep_list = M('category')->where($where)->field('id,pid,name')->findAll();
		echo json_encode($dep_list);
	}
	/**
	 * 用户禁用
	 */
	function prohibit(){
		$uid = trim($_GET['uid']);
		$data['state'] = 0;
		$where = "uid='$uid' AND uid<>1";
		$User = M('User');
		$User->data($data)->where($where)->save();
		$this->redirect('User/ulist');
	}
	/**
	 * 用户删除
	 */
	function del(){
		$uid = trim($_GET['uid']);
		$where = "uid='$uid' AND uid<>1";
		$data['state'] = -1;
		$User = M('User');
		$User->data($data)->where($where)->save();
		$this->redirect('User/ulist');
	}
}
?>